﻿Ext.onReady(function () {
    PageSafe();

    //人员列表
    var rolestore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('BaseManager.RoleView', 'GetRoles'),
        root: 'ResponseObject',
        fields: ['PID', 'RoleName', 'Creater', 'LastUpdate', 'NTime', 'CreateName.UserName']
    });
    rolestore.load();

    //地区树
    var AreaTree = new Ext.tree.TreePanel({
        width: 260,
        style: 'padding-left:20px',
        border: false,
        height: 300,
        split: true,
        rootVisible: true,
        useArrows: false, /*去掉树线*/
        autoScroll: true,
        collapseMode: 'mini',
        root: {},
        listeners: {
            render: function () {
                renderTree(AreaTree); /*渲染菜单树*/
            },
            checkchange: function (node, state) {
                if (state) {
                    //                    CurrentArea = node.attributes;
                    //                    Ext.getCmp("AreaName1").setText(CurrentArea.text);
                    //                    Ext.getCmp("ChargeName").setText(CurrentArea.AreaChargePerson.UserName);
                    //                    Ext.getCmp("Remark").setText(CurrentArea.AreaRemark);
                }
            }
        }
    });

    var personstore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('BaseManager.PersonManager.PersonView', 'GetPersonList'),
        root: 'ResponseObject',
        fields: ['PID', 'UserAccount', 'UserName', 'ChargeArea.AreaName', 'Company', 'Tel', 'Email', 'Age', 'NTime', 'UserInto', 'Job', 'RoleName'],
        listeners: {
            beforeload: function (st, op) {
                var re = new JsonRequest();
                re.category = CATEGORY.READONLY;
                st.baseParams = re;
                st.proxy.conn.jsonData = re;
            }
        }
    });
    var personsm = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true
    });
    var persongrid = new Ext.grid.GridPanel({
        bodyStyle: 'padding:2px',
        border: false,
        split: true,
        height: 500,
        sm: personsm,
        loadMask: true, //加载
        columns: [
       personsm,
       { id: 'UserAccount', header: '用户名', width: 100, dataIndex: 'UserAccount', sortable: true },
       { id: 'UserName', header: '用户姓名', width: 100, dataIndex: 'UserName', sortable: true },
       { id: 'ChargeArea.AreaName', header: '所属地区', width: 100, dataIndex: 'ChargeArea.AreaName', sortable: true },
       { id: 'RoleName', header: '所属角色', width: 100, dataIndex: 'RoleName', sortable: true },
       { id: 'Company', header: '公司名称', width: 100, dataIndex: 'Company', sortable: true },
       { id: 'Tel', header: '联系电话', width: 100, dataIndex: 'Tel', sortable: true },
       { id: 'Email', header: '电子邮箱', width: 100, dataIndex: 'Email', sortable: true },
       { id: 'Age', header: '年龄', width: 100, dataIndex: 'Age', sortable: true },
       { id: 'Job', header: '职称', width: 100, dataIndex: 'Job', sortable: true },
       { id: 'UserInto', header: '分成比例', width: 100, dataIndex: 'UserInto', sortable: true },
       { id: 'NTime', header: '注册时间', width: 100, dataIndex: 'NTime', sortable: true }
   ],
        store: personstore
    });

    var Page = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'center',
            title: '人员管理',
            iconCls: 'personwin-icon',
            tbar: [{ text: '增加人员', iconCls: 'add-icon', handler: function () {
                var win = new Ext.Window({
                    width: 400,
                    resizable: false,
                    modal: true,
                    height: 360,
                    title: '人员新增',
                    iconCls: 'personadd-icon',
                    layout: 'form',
                    bodyStyle: 'padding: 10px',
                    defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                    labelAlign: 'right',
                    items: [{
                        fieldLabel: '用户名',
                        width: 100,
                        id: 'UserAccount',
                        name: 'UserAccount'
                    }, {
                        fieldLabel: '用户姓名',
                        width: 100,
                        id: 'UserName',
                        name: 'UserName'
                    }, {
                        fieldLabel: '所在地址',
                        width: 200,
                        xtype: 'combotree',
                        id: 'Address',
                        allowUnLeafClick: false,
                        passName: 'Address',
                        treeHeight: 300,
                        tree: AreaTree
                    }, {
                        fieldLabel: '所属角色',
                        width: 150,
                        xtype: 'combo',
                        id: 'Role',
                        displayField: 'RoleName',
                        valueField: 'PID',
                        selectOnFocus: true,
                        triggerAction: 'all',
                        store: rolestore
                    }, {
                        fieldLabel: '公司名称',
                        width: 150,
                        id: 'Company',
                        name: 'Company'
                    }, {
                        fieldLabel: '联系电话',
                        width: 100,
                        id: 'Tel',
                        name: 'Tel'
                    }, {
                        fieldLabel: '电子邮件',
                        width: 120,
                        id: 'EMail',
                        name: 'EMail'
                    }, {
                        fieldLabel: '年龄',
                        width: 50,
                        id: 'Age',
                        name: 'Age'
                    }, {
                        fieldLabel: '职称',
                        width: 80,
                        id: 'Job',
                        name: 'Job'
                    }, {
                        fieldLabel: '分成比例',
                        width: 50,
                        id: 'UserInto',
                        name: 'UserInto',
                        text:'0'
                    }],
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                        if (Ext.getCmp("UserAccount").getValue() == "") { Ext.Msg.alert("提示信息", "请输入用户帐号"); return; }
                        if (Ext.getCmp("Address").passField.value == "") { Ext.Msg.alert("提示信息", "请输入选择用户所在地区"); return; }
                        if (Ext.getCmp("Role").getValue() == "") { Ext.Msg.alert("提示信息", "请输入用户所属角色"); return; }
                        if (Ext.getCmp("UserInto").getValue() == "") { Ext.Msg.alert("提示信息", "请输入用户分成比例"); return; }
                        var re = new JsonRequest();
                        re.params.UserInfo = {
                            UserAccount: Ext.getCmp("UserAccount").getValue(),
                            UserName: Ext.getCmp("UserName").getValue(),
                            Address: Ext.getCmp("Address").passField.value,
                            Company: Ext.getCmp("Company").getValue(),
                            Tel: Ext.getCmp("Tel").getValue(),
                            Email: Ext.getCmp("EMail").getValue(),
                            Age: Ext.getCmp("Age").getValue(),
                            Job: Ext.getCmp("Job").getValue(),
                            RoleID: Ext.getCmp("Role").getValue(),
                            UserInto: Ext.getCmp("UserInto").getValue()
                        }
                        Ext.Ajax.request({
                            url: Ext.getClassUrl('BaseManager.PersonManager.PersonView', 'AddPerson'),
                            jsonData: re,
                            success: function (re, op) {
                                var obj = Ext.decode(re.responseText);
                                if (obj.ResponseObject != null) {
                                    Ext.Msg.alert("用户管理提示", "用户已经增加成功，初始化密码是123456", function () { personstore.load(); win.close(); });
                                }
                                else {
                                    Ext.Msg.alert("用户管理提示", "用户已经增加失败，请联系管理员", function () { personstore.load(); win.close(); });
                                }
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { win.close(); } }]
                }).show();
            }
            }, { text: '修改人员', iconCls: 'update-icon', handler: function () {
                var v = persongrid.getSelectionModel().selections.items;
                if (v.length > 0) {
                    var upwin = new Ext.Window({
                        width: 400,
                        resizable: false,
                        modal: true,
                        height: 360,
                        title: '人员新增',
                        iconCls: 'personadd-icon',
                        layout: 'form',
                        bodyStyle: 'padding: 10px',
                        defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                        labelAlign: 'right',
                        items: [{
                            fieldLabel: '用户名',
                            width: 100,
                            id: 'U_UserAccount',
                            name: 'U_UserAccount'
                        }, {
                            fieldLabel: '用户姓名',
                            width: 100,
                            id: 'U_UserName',
                            name: 'U_UserName'
                        }, {
                            fieldLabel: '所在地址',
                            width: 200,
                            xtype: 'combotree',
                            id: 'u_Address',
                            allowUnLeafClick: false,
                            passName: 'Address',
                            treeHeight: 300,
                            tree: AreaTree
                        }, {
                            fieldLabel: '所属角色',
                            width: 150,
                            xtype: 'combo',
                            id: 'u_Role',
                            displayField: 'RoleName',
                            valueField: 'PID',
                            selectOnFocus: true,
                            triggerAction: 'all',
                            store: rolestore
                        }, {
                            fieldLabel: '公司名称',
                            width: 150,
                            id: 'U_Company',
                            name: 'U_Company'
                        }, {
                            fieldLabel: '联系电话',
                            width: 100,
                            id: 'U_Tel',
                            name: 'U_Tel'
                        }, {
                            fieldLabel: '电子邮件',
                            width: 120,
                            id: 'U_EMail',
                            name: 'U_EMail'
                        }, {
                            fieldLabel: '年龄',
                            width: 50,
                            id: 'U_Age',
                            name: 'U_Age'
                        }],
                        buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                            var re = new JsonRequest();
                            re.params.UserInfo = {
                                PID: v[0].json.PID,
                                UserAccount: Ext.getCmp("U_UserAccount").getValue(),
                                UserName: Ext.getCmp("U_UserName").getValue(),
                                Address: Ext.getCmp("u_Address").passField.value,
                                Company: Ext.getCmp("U_Company").getValue(),
                                Tel: Ext.getCmp("U_Tel").getValue(),
                                Email: Ext.getCmp("U_EMail").getValue(),
                                Age: Ext.getCmp("U_Age").getValue(),
                                RoleID: Ext.getCmp("u_Role").getValue()
                            }
                            Ext.Ajax.request({
                                url: Ext.getClassUrl('BaseManager.PersonManager.PersonView', 'UpdatePerson'),
                                jsonData: re,
                                success: function (re, op) {
                                    var obj = Ext.decode(re.responseText);
                                    if (obj.ResponseObject != null) {
                                        Ext.Msg.alert("用户管理提示", "用户已经修改成功", function () { personstore.load(); upwin.close(); });
                                    }
                                    else {
                                        Ext.Msg.alert("用户管理提示", "用户修改失败，请联系管理员", function () { personstore.load(); upwin.close(); });
                                    }
                                }
                            });
                        }
                        }, { text: '取消', iconCls: 'cancel-icon', handler: function () { upwin.close(); } }],
                        listeners: {
                            render: function () {
                                Ext.getCmp("U_UserAccount").setValue(v[0].json.UserAccount);
                                Ext.getCmp("U_UserName").setValue(v[0].json.UserName);
                                Ext.getCmp("u_Address").setValue(v[0].json.ChargeArea.AreaName);
                                Ext.getCmp("U_Company").setValue(v[0].json.Company);
                                Ext.getCmp("U_Tel").setValue(v[0].json.Tel);
                                Ext.getCmp("U_EMail").setValue(v[0].json.Email);
                                Ext.getCmp("U_Age").setValue(v[0].json.Age);
                                Ext.getCmp("u_Role").setValue(v[0].json.RoleID);
                            }
                        }
                    }).show();
                }
                else {
                    Ext.Msg.alert("用户管理提示", "请选择一个需要修改的人员");
                }
            }
            }, { text: '删除人员', iconCls: 'del-icon', handler: function () {
                var v = persongrid.getSelectionModel().selections.items;
                if (v.length > 0) {
                    var re = new JsonRequest();
                    re.params.PID = v[0].json.PID;
                    Ext.Ajax.request({
                        url: Ext.getClassUrl('BaseManager.PersonManager.PersonView', 'DelPerson'),
                        jsonData: re,
                        success: function (re, op) {
                            var obj = Ext.decode(re.responseText);
                            if (obj.ResponseObject != null) {
                                Ext.Msg.alert("用户管理提示", "用户已经删除成功", function () { personstore.load(); });
                            }
                            else {
                                Ext.Msg.alert("用户管理提示", "用户删除失败，请联系管理员", function () { personstore.load(); });
                            }
                        }
                    });
                }
                else {
                    Ext.Msg.alert("用户管理提示", "请选择一个需要删除的人员");
                }
            }
            }],
            items: persongrid
        }],
        listeners: {
            render: function () {
                personstore.load();
            }
        }
    });
});

//初始化树
var renderTree = function (tree) {
    var re = new JsonRequest();
    re.category = CATEGORY.READONLY;
    re.params.param = {};
    Ext.Ajax.request({
        url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'AreaData'),
        jsonData: re,
        success: function (re, op) {
            var obj = Ext.decode(re.responseText).ResponseObject;
            var ns = {}, f = tree;
            ns.Children = obj;
            var r = new Ext.tree.TreeNode({
                id: '0',
                text: '全部地区'
            });
            var selectedNode = null, selectedId = f.selectedId;
            function appendChild(node, o) {
                if (node.PID == selectedId) {
                    selectedNode = node;
                }
                if (o.Children != null && o.Children.length > 0) {
                    for (var a = 0; a < o.Children.length; a++) {
                        var n = new Ext.tree.TreeNode({
                            id: o.Children[a].PID,
                            icon: '../images/menu/ui-text-area.png',
                            text: o.Children[a].AreaName,
                            parentId: o.Children[a].ParentID,
                            AreaRemark: o.Children[a].AreaRemark,
                            PersonID: o.Children[a].PersonID,
                            AreaChargePerson: o.Children[a].AreaChargePerson
                        });
                        node.appendChild(n);
                        appendChild(n, o.Children[a]);

                    }
                }
            }
            appendChild(r, ns);
            f.setRootNode(r);

            tree.registerNode(r); /*注册节点, 不然getNodeById(node.id)找不到这个节点*/
            tree.root.expand(true);
        }
    })
}